package com.ca.ui.core.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.ca.ui.core.R;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import jp.wasabeef.glide.transformations.BlurTransformation;

public class ImageUtils {

    public static void loadImage(Context context, String imageUrl, ImageView view) {
        loadRoundImage(context, imageUrl, view, 1);
    }

    public static void loadCircleImage(Context context, String imageUrl, ImageView view) {
        BitmapTransformation transformation = new CircleCrop();
        loadImage(context, imageUrl, view, transformation);
    }

    public static void loadRoundImage(Context context, String imageUrl, ImageView view) {
        loadRoundImage(context, imageUrl, view, 20);
    }

    public static void loadRoundImage(Context context, String imageUrl, ImageView view, int radius) {
        BitmapTransformation transformation = new RoundedCorners(radius);
        loadImage(context, imageUrl, view, transformation);
    }

    private static void loadImage(Context context, String imageUrl, ImageView view, BitmapTransformation transformation) {
        Glide.with(context)
                .load(imageUrl)
//                .listener(new RequestListener<Drawable>() {
//                    @Override
//                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
//                        return false;
//                    }
//
//                    @Override
//                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
//
//                        FileOutputStream out = null;
//                        try {
//                            File file = File.createTempFile(imageUrl, null, context.getCacheDir());
//                            out = new FileOutputStream(file);
//                            ((BitmapDrawable) resource).getBitmap().compress(Bitmap.CompressFormat.JPEG, 100, out);
//                            out.close();
//                        }
//                        catch (Exception e) {
//                            Log.e("CacheImage Failed", e.getMessage());
//                        }
//                        finally {
//                            if (out != null) {
//                                try {
//                                    out.close();
//                                } catch (IOException e) {
//                                    e.printStackTrace();
//                                }
//                            }
//                        }
//
//                        return false;
//                    }
//                })
                .transition(DrawableTransitionOptions.withCrossFade())
                .error(R.drawable.ic_error_image)
                .transform(transformation)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(view);
    }

    public static void loadFuzzyImage(Context context, String imageUrl, ImageView imageView, int fuzzy) {
        Glide.with(context)
                .load(imageUrl)
//                .listener(new RequestListener<Drawable>() {
//                    @Override
//                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
//                        return false;
//                    }
//
//                    @Override
//                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
//
//                        FileOutputStream out = null;
//                        try {
//                            File file = File.createTempFile(imageUrl, null, context.getCacheDir());
//                            out = new FileOutputStream(file);
//                            ((BitmapDrawable) resource).getBitmap().compress(Bitmap.CompressFormat.JPEG, 100, out);
//                            out.close();
//                        }
//                        catch (Exception e) {
//                            Log.e("CacheImage Failed", e.getMessage());
//                        }
//                        finally {
//                            if (out != null) {
//                                try {
//                                    out.close();
//                                } catch (IOException e) {
//                                    e.printStackTrace();
//                                }
//                            }
//                        }
//
//                        return false;
//                    }
//                })
                .transition(DrawableTransitionOptions.withCrossFade())
                .error(R.drawable.ic_error_image)
                .apply(RequestOptions.bitmapTransform(new BlurTransformation(25, fuzzy)))
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }
}
